package com.ant.cn.views;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.ant.ut.utils.AntLog;

/**
 * Created by Administrator on 2016/12/2.
 */

public class ProgressViews extends View {

    private Paint paint,paint1, paint2, paint3;
    private int rad = 150, start = 0, end = 80;
    private int vwidth, vheight;
    private Point center = new Point();
    private RectF rec = new RectF(50, 50, 200, 200);

    public ProgressViews(Context context) {
        super(context);
        initThisView(context, null, 0);
    }

    public ProgressViews(Context context, AttributeSet attrs) {
        super(context, attrs);
        initThisView(context, attrs, 0);
    }

    public ProgressViews(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initThisView(context, attrs, defStyleAttr);
    }

    private void initThisView(Context context, AttributeSet attrs, int defStyleAttr) {
        initData();

    }

    private void initData() {
        paint = getPaint(Color.RED, 3);
        paint1 = getPaint(Color.rgb(160,8,160), 10);
        paint2 = getPaint(Color.GREEN, 10);
        paint3=getPaint(Color.LTGRAY,10);
    }

    /**
     * user:get a paint for color ant width
     *
     * @param color color of paint
     * @param width width of paint
     * @return return a new  paint
     */
    private Paint getPaint(int color, int width) {
        Paint paint = new Paint();
        paint.setColor(color);
        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(width);
        return paint;

    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        AntLog.i("onFinishInflate....");
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(mesureWidth(widthMeasureSpec), mesureHeith(heightMeasureSpec));
        AntLog.i("onMeasure  wi=" + widthMeasureSpec + ",  he=" + heightMeasureSpec);
    }

    private int mesureHeith(int heightMeasureSpec) {

        return heightMeasureSpec;
    }

    private int mesureWidth(int widthMeasureSpec) {
        int result = 0;
        int model = MeasureSpec.getMode(widthMeasureSpec);
        int size = MeasureSpec.getSize(widthMeasureSpec);

        if (MeasureSpec.EXACTLY == model) {
            result = size;
        } else {
            result = 200;
            if (model == MeasureSpec.AT_MOST)
                result = Math.max(result, size);
        }

        return result;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        AntLog.i("onSizeChange=   w=" + w + ",  h=" + h + ",  oldW=" + oldw + ",  oldh=" + oldh);
        vwidth = w;
        vheight = h;
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        AntLog.i("onLayout   changer=" + changed + ", left=" + left + ", top=" + top + ", right" + right + ", bottom=" + bottom);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
//        canvas.drawCircle(vwidth >> 1, vheight >> 1, rad, paint);
//        AntLog.i("onDraw..");
        canvas.drawRect(rec,paint3);
        canvas.drawArc(rec, 0, 360, false, paint2);
        canvas.drawArc(rec, start, 45, false, paint1);
        AntLog.i("arc start=" + start + ",  end=" +(start+45));
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
////                rad += 2;
//                AntLog.i("rad=" + rad);
//                start += 10;
//                start %= 360;

                ValueAnimator va=ValueAnimator.ofInt(0,360);
                va.setDuration(1000);
                va.setRepeatCount(9);
                va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        int value= (int) animation.getAnimatedValue();
                        start=value;
                        start%=360;
                        invalidate();
                    }
                });
                va.start();
                invalidate();
                break;
        }
        return true;
    }

}
